// pages/mine/inviteFriends/inviteFriends.ts
import drawQrcode from '../../../utils/weapp.qrcode'
Page({
    data: {
        // 设备宽高
        windowW: 0,
        windowH: 0,
        propic: '',
        currentLineHeight: 0,
        preurl: "", //图片路径
    },
    onLoad() {
        let that = this
        // 获取设备宽高
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    windowW: res.windowWidth,
                    windowH: res.windowHeight
                })
            }
        })
        that.setData({
            propic: '../../image/mine/inviteBg.png',
        })
    },
    // 页面渲染完成，生成二维码
    onReady() {
        const that = this;
        // 画二维码
        drawQrcode({
            width: this.createRpx2px(380),
            height: this.createRpx2px(380),
            canvasId: 'myQrcode',
            text: 'https://backend.kindstaryouyi.com/',  //扫描跳转路径
            callback() {
                wx.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: that.createRpx2px(380),
                    height: that.createRpx2px(380),
                    destWidth: that.createRpx2px(300),
                    destHeight: that.createRpx2px(300),
                    canvasId: 'myQrcode',
                    success: function (res) {
                        let filePath = res.tempFilePath;
                        console.log(res.tempFilePath)
                        // 绘画图片
                        that.drawCanvas(filePath)
                    },
                })
            }
        })
    },
    // 转化单位进制
    createRpx2px(rpx: number) {
        return wx.getSystemInfoSync().windowWidth / 750 * rpx
    },
    // 用户分享给微信
    onShareAppMessage() {
        return {
            title: '分享',
            path: '/pages/index/index',
            // imageUrl: '/resources/goods/1.png'
        }
    },
    // 绘画图片
    drawCanvas(filePath: any) {
        let that = this
        // 获取设备宽高
        let windowW = that.data.windowW  //375px
        let windowH = that.data.windowH  //667px
        let rowNum = 0
        // 获取绘图上下文对象
        let ctx = wx.createCanvasContext('firstCanvas')
        that.setData({
            currentLineHeight: 460 - rowNum * 26
        })
        // 背景图
        ctx.drawImage(that.data.propic, (windowW - 279) / 2, 32, 279, that.data.currentLineHeight)
        // 说明文字
        ctx.setFontSize(14)
        ctx.setFillStyle("#999999")
        ctx.setTextAlign('center')  //文字居中
        ctx.fillText('被邀请者，直接点击该海报链接浏览', (windowW - 279) / 2 + 279 / 2, 84)
        ctx.fillText('器打开下载“医检-医生”APP', (windowW - 279) / 2 + 279 / 2, 104)
        // 二维码
        ctx.rect((windowW - 279) / 2 + 40, 32 + 168, 200, 200)
        ctx.setFillStyle('#FFFFFF')
        ctx.fill()
        ctx.drawImage(filePath, (windowW - 279) / 2 + 45, 32 + 168 + 5, 190, 190)
        ctx.draw()
    },
    // 点击保存图片
    savePic() {
        let that = this
        // 生成加载中
        wx.showLoading({
            title: '努力生成中...'
        })
        wx.canvasToTempFilePath({
            x: (that.data.windowW - 279) / 2,
            y: 32,
            width: 279,
            height: that.data.currentLineHeight,
            canvasId: 'firstCanvas',
            success: function (res) {
                // console.log(res.tempFilePath)
                that.setData({
                    preurl: res.tempFilePath,
                })
                setTimeout(() => {
                    that.saveQrcode()
                }, 200);
            },
            fail: function (res) {
                // 如果生成图片失败，也会停止加载
                wx.hideLoading()
            }
        })
    },
    // 准备存储图片
    saveQrcode() {
        const that = this;
        // 获取手机权限
        wx.getSetting({
            success: (res) => {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    // 如果之前没有开启存储到相册的权限，就请求权限
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success: (res) => {
                            wx.hideLoading()
                            this.eventSave()
                        },
                        fail: err => {
                            wx.hideLoading()
                            if (err.errMsg.includes('authorize:fail')) {
                                // 重新打开相册权限
                                wx.openSetting({
                                    success(settingdata) {
                                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                            // 获取权限成功，给出再次点击图片保存到相册的提示
                                            that.eventSave()
                                        } else {
                                            // 获取权限失败，给出不给权限就无法正常使用的提示
                                        }
                                    },
                                    // 打开权限失败，目前小程序无法在用户未操作的情况下，打开设置页
                                    fail() {
                                        wx.showToast({
                                            icon: "none",
                                            title: "授权失败！请打开小程序-设置，打开存储相册权限"
                                        })
                                    }
                                })
                            }
                        }
                    })
                } else {
                    this.eventSave();
                    wx.hideLoading()
                }
            }
        })
        // this.setData({ showInvitePatient: false })
    },
    // 保存图片到相册
    eventSave() {
        let that = this
        wx.saveImageToPhotosAlbum({
            filePath: that.data.preurl,
            success(res) {
                wx.showToast({
                    title: '保存图片成功',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    }
})